package csm.sql.pool;

import java.sql.Connection;
import java.sql.SQLException;

import oracle.jdbc.pool.OracleDataSource;
import csm.sql.ConnectionFactory;
import csm.sql.DBConfig;
import csm.sql.SmartConnection;

/**
 * 
 * @author seungmun
 * @since 2006.1.18.
 * @version
 * @see
 */
public class Oracle11gConnectionPool extends ConnectionFactory
{
	OracleDataSource ods;

	@Override
	protected void createConnectionPool() throws SQLException
	{
		try
		{
			DBConfig dbc = DBConfig.getDBConfig(this.connectionName);

			ods = new OracleDataSource();

			ods.setURL(dbc.getSettings("connection.url"));
			ods.setUser(dbc.getSettings("connection.user"));
			ods.setPassword(dbc.getSettings("connection.password"));

			// TODO 아래한줄 복원필요
			// ods.setFastConnectionFailoverEnabled(true);

			ods.setExplicitCachingEnabled(true);
			java.util.Properties prop = new java.util.Properties();
			prop.setProperty("MinLimit", dbc.getSettings("connection.minimumPoolSize"));
			prop.setProperty("MaxLimit", dbc.getSettings("connection.maximumPoolSize"));
			prop.setProperty("TimeToLiveTimeout", dbc.getSettings("connection.timeToLiveTimeout"));

			// TODO 아래한줄 복원필요
			// ods.setConnectionProperties(prop);
		}
		catch (SQLException ex)
		{
			throw new SQLException(ex.toString() + " in OracleConnectionPool.createConnectionPool()", ex.getSQLState(), ex.getErrorCode());
		}
	}

	public Oracle11gConnectionPool()
	{
	}

	@Override
	public Connection getConnection() throws SQLException
	{
		try
		{
			if (ods == null) 
				createConnectionPool();
			
			Connection conn = ods.getConnection();
			conn.setAutoCommit(true);
			return new SmartConnection(conn);
		}
		catch (SQLException ex)
		{
			throw new SQLException(ex.toString() + " in OracleConnectionPool.getConnection()", ex.getSQLState(), ex.getErrorCode());
		}
	}

	@Override
	public void closeConnectionPool() throws SQLException
	{
		ods = null;
	}

}
